﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using com.subb.WebFramework;
using WebEntities;
using WebEntities.Models.Framework;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class MemGroupController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllMemGroups(StoreRequestParameters parms)
        {
            FW_MemGroupDAL gdal = new FW_MemGroupDAL();
            int ttlCount;
            List<FW_MemGroup> dbgrplist = gdal.GetAllMemGroups(parms.Start, parms.Limit, out ttlCount);
            List<MemGroup> grplist = new List<MemGroup>();
            foreach (var dbgrp in dbgrplist)
            {
                MemGroup grp = MemGroup.AsMemGroup(dbgrp);
                grplist.Add(grp);
            }
            return this.Store(grplist, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteMemGroup(List<MemGroup> delMemGrps)
        {
            try
            {
                FW_MemGroupDAL gdal = new FW_MemGroupDAL();
                foreach (var grp in delMemGrps)
                {
                    gdal.RemoveMemGroup(grp.GroupID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult CreateMemGroup()
        {
            return View();
        }

        [HttpPost]
        public FormPanelResult CreateMemGroup(MemGroup memgrp)
        {
            //新建用户群组
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MemGroupDAL gdal = new FW_MemGroupDAL();
                memgrp.Creator = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                gdal.CreateMemGroup(memgrp);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "群组创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult NewMemGroupWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建群组",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.GroupAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateMemGroup"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            //wnd.Listeners.Close.Handler = "App.GridPanel1.store.reload();";
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditMemGroupWindow(int grpId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑群组",
                Width = 800,
                Height = 500,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.User,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditMemGroup"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("grpId", grpId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditMemGroup(int grpId)
        {
            FW_MemGroupDAL gdal = new FW_MemGroupDAL();
            FW_MemGroup dbgrp = gdal.GetMemGroup(grpId);
            MemGroup grp = MemGroup.AsMemGroup(dbgrp);
            return View(grp);
        }
    }
}
